import Vue from 'vue';
import VueRouter from 'vue-router'
import { Message } from 'element-ui';
import axios from 'axios'
import Enter from '../views/Enter.vue'

Vue.use(VueRouter)
  // 路由嵌套 动态路由

  const routes = [

    // 案例一
    {
      path: '/:website_id/',
      component: Enter,
      children: [
        // 首页
        {
          path: '/',
          component: ()=> import('../views/EaseCase/Home.vue')
        },
        // 课程列表
        {
          path: 'CurriculumSchedule',
          component: ()=> import('../views/EaseCase/CurriculumSchedule.vue')
        },
        // 关于我们
        {
          path: 'AboutWe',
          component:  ()=> import('../views/EaseCase/AboutWe.vue')
        },
        // 文章详情
        {
          path: 'ArticleDetail',
          component:  ()=> import('../views/EaseCase/ArticleDetail.vue')
        },
        // 我的页面 
        {
          path: 'MyPage',
          component:  ()=> import('../views/EaseCase/MyPage.vue')
        },
      ]
    },
    { path: '*', component: ()=> import('../views/404.vue') },
  ]

const router = new VueRouter({
  mode: 'history',
  base: '/EaseCase/', 
  routes,
 
})
router.beforeEach((to, from, next)=> {
  console.log(to, '导航守卫部分',from)
  if (to.params.website_id) {
    axios.post('/interfaces/mini_apps/exist',{
      website_id: to.params.website_id,
    }).then((res)=> {
      if (res.data.code == 200) {
        next(true);
      } else {
        Message({
          message: '站点不存在，正在跳转',
          type: 'warning'
        });

        setTimeout(()=> {
          // window.location.href = 'https://jzt.china9.cn';
        },2000)
      }
    })
  }  else {
    Message({
      message: '站点不存在，正在跳转',
      type: 'warning'
    });
    setTimeout(()=> {
      // window.location.href = 'https://jzt.china9.cn';
    },2000)
  }
})

export default router
